package com.njlime.cms.service.sys;

import com.njlime.cms.entities.InformationComment;
import com.njlime.cms.repository.sys.InformationCommentDao;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.member.entiies.MemberComment;
import com.njlime.member.service.sys.MemberService;
import com.njlime.sys.security.util.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 热点相关Service
 * Created by Dean on 2017-8-9.
 */
@Service
@Transactional(readOnly = true)
public class InformationCommentService {

    @Resource
    private InformationCommentDao informationCommentDao;
    @Resource
    private InformationService informationService;
    @Resource
    private MemberService memberService;

    /**
     * 获取详情
     * @param id
     * @return
     */
    public InformationComment findOne(Integer id){
        return informationCommentDao.findOne(id);
    }

    /**
     * 删除
     * @return
     */
    @Transactional(readOnly = false)
    public int del(InformationComment comment) {
        int count = informationCommentDao.del(comment.getId());
        comment = informationCommentDao.findOne(comment.getId());
        if(comment.getStatus() == GlobalValue.AUDITED){
            informationService.updateCommentNum(comment.getInformation_id(), -1);
        }
        return count;
    }

    /**
     * 列表
     * @param pageBean
     * @param comment
     * @return
     */
    public PageBean list(PageBean pageBean, InformationComment comment) {
        return informationCommentDao.list(pageBean, comment);
    }

    /**
     * 点赞
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int like(int id ){
        return informationCommentDao.like(id);
    }

    /**
     * 评论置顶取消置顶
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int top(int id ){
        return informationCommentDao.top(id);
    }

    /**
     * 回复
     * @param id
     * @param reply
     * @return
     */
    @Transactional(readOnly = false)
    public int reply(int id ,String reply){
        return informationCommentDao.reply(id,reply, UserUtils.getUser().getId());
    }

    /**
     * 审核
     * @param comment
     */
    @Transactional(readOnly = false)
    public void auditComment(InformationComment comment, MemberComment memberComment) {
        informationCommentDao.auditComment(comment.getId());
        memberService.saveComment(memberComment);
        informationService.updateCommentNum(comment.getInformation_id(), 1);
    }


}
